package com.dream.service.recommender.splitter.computer.impl;

import com.dream.service.recommender.domain.splitter.DefaultConfig;
import com.dream.service.recommender.domain.splitter.ExpConfig;
import com.dream.service.recommender.domain.splitter.PlacementConfig;
import com.dream.service.recommender.domain.splitter.SplitterRequest;
import com.dream.service.recommender.domain.splitter.enums.StrategyType;
import com.dream.service.recommender.splitter.computer.Computer;
import com.dream.service.recommender.splitter.strage.Strategy;
import com.dream.service.recommender.splitter.strage.StrategyFactory;
import com.dream.service.recommender.splitter.zk.ZkOperator;
import com.dream.service.recommender.toolkits.utils.ConfigUtil;
import com.dream.service.recommender.toolkits.utils.StringUtils;

import java.util.List;

/**
 * Created by zhiji on 2016/3/15.
 */
public class SplitterComputer implements Computer {

    private ZkOperator zkOperator;
    private String basezkpath = ConfigUtil.getPro("base_config_path", "/em/recommenders/");

    public ZkOperator getZkOperator() {
        return zkOperator;
    }

    public void setZkOperator(ZkOperator zkOperator) {
        this.zkOperator = zkOperator;
    }

    @Override
    public ExpConfig getExp(long recomid, SplitterRequest request) throws Exception {
        PlacementConfig placementConfig = zkOperator.getData(basezkpath + recomid);
        if (placementConfig == null) {
            throw new RuntimeException("无法获取推荐位的配置信息");
        }
        DefaultConfig defaultConfig = placementConfig.getDefault_config();
        List<ExpConfig> expConfigList = placementConfig.getExp_config_list();
        if (StringUtils.isEmpty(expConfigList)) {
            return defaultConfig;
        }
        StrategyType strategyType = defaultConfig.getStrategyType();
        Strategy strategy = StrategyFactory.getStrager(strategyType);
        ExpConfig expConfig = strategy.getCurrentExpConfig(placementConfig, request);
        return expConfig == null ? defaultConfig : expConfig;
    }
}
